﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Amazon.DynamoDB;
using Amazon.DynamoDB.DataModel;
using Amazon.Runtime;
using Amazon.SecurityToken;
using DevToolBox.Common;

namespace DevToolBox.Cloud.Amazon
{
    /// <summary>
    /// Class to encapsulate the specific aspects of building an 
    /// Amazon data base client object for query/scan purposes
    /// </summary>
    public class DynamoDbClient
    {
        protected internal AmazonDynamoDB Client { get; set; }
        protected internal int ReadCapacityUnits { get; set; }
        protected internal int WriteCapacityUnits { get; set; }

        /// <summary>
        /// Default constructor
        /// </summary>
        public DynamoDbClient()
        {
            string proxyHost =ConfigParamReader.GetString("AmazonProxyHost");
            int proxyPort = ConfigParamReader.GetInt32("AmazonProxyPort");
            string proxyUsername = ConfigParamReader.GetString("AmazonProxyUsername");
            string proxyPassword = ConfigParamReader.GetString("AmazonProxyPassword");
            int readCapacity =  ConfigParamReader.GetInt32("AmazonReadCapacity");
            int writeCapacity = ConfigParamReader.GetInt32("AmazonWriteCapacity");
            string dynamoDbServiceUrl = ConfigParamReader.GetString("DynamoEndPoint");

            AmazonDynamoDBConfig config = new AmazonDynamoDBConfig();
            config.UseSecureStringForAwsSecretKey = false;
            config.ProxyPassword = proxyPassword;
            config.ProxyUsername = proxyUsername;
            config.ProxyHost = proxyHost;
            config.ProxyPort = proxyPort;            
            config.ServiceURL = dynamoDbServiceUrl;
            ReadCapacityUnits = readCapacity;
            WriteCapacityUnits = writeCapacity;

            string secretKey = ConfigParamReader.GetString("AWSSecretKey");
            string accessKey = ConfigParamReader.GetString("AWSAccessKey");
            AmazonSecurityTokenServiceConfig stsConfig = new AmazonSecurityTokenServiceConfig();
            stsConfig.UseSecureStringForAwsSecretKey = false;
            var sessionCredentials = new RefreshingSessionAWSCredentials(accessKey, secretKey, stsConfig);
            Client = new AmazonDynamoDBClient(sessionCredentials, config);
        }
    }

}
